package com.cari.oa.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.OARepositoryEntity;
import com.cari.oa.util.StrUtils;
@Repository
@SuppressWarnings("unchecked")
public class OARepositoryDao extends BaseDao<OARepositoryEntity> {
	
	/**
	 * 分页查询记录
	 * @author -- liuyoujie
	 * @2013-6-17 上午10:53:46
	 * @param title		标题
	 * @param creator	创建人
	 * @param beginTime	
	 * @param endTime
	 * @param page
	 * @param size
	 * @return
	 */
	public List<OARepositoryEntity> getPage(String title, String content, String creator, 
			Timestamp beginTime, Timestamp endTime, Integer page, Integer size){
		StringBuilder stringBuilder = new StringBuilder("from OARepositoryEntity o where 1=1 ");
		
		if(StrUtils.isNotEmpty(title))	stringBuilder.append(" and o.title like :title ");
		if(StrUtils.isNotEmpty(creator)) stringBuilder.append(" and o.creator like :creator ");
		if(StrUtils.isNotEmpty(content)) stringBuilder.append(" and o.content like :content ");
		if(beginTime != null) stringBuilder.append(" and o.createTime > :beginTime ");
		if(endTime != null)	stringBuilder.append(" and o.createTime < :endTime ");
		stringBuilder.append(" order by o.createTime desc");
		
		Query query = getSession().createQuery(stringBuilder.toString());
		
		if(StrUtils.isNotEmpty(title)) query.setParameter("title", "%" + title + "%");
		if(StrUtils.isNotEmpty(content)) query.setParameter("content", "%" + content + "%");
		if(StrUtils.isNotEmpty(creator))	query.setParameter("creator", "%" + creator + "%");
		if(beginTime != null)	query.setParameter("beginTime", beginTime);
		if(endTime != null)		query.setParameter("endTime", endTime);
		
		query.setFirstResult((page - 1)*size).setMaxResults(size);
		
		return query.list();
	}
	
	/**
	 * 查询记录总数
	 * @author -- liuyoujie
	 * @2013-6-17 上午10:54:16
	 * @param title
	 * @param creator
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public long getCount(String title, String content, String creator, 
			Timestamp beginTime, Timestamp endTime){
		StringBuilder stringBuilder = new StringBuilder("select count(*) from OARepositoryEntity o where 1=1 ");
		
		if(StrUtils.isNotEmpty(title))	stringBuilder.append(" and o.title like :title ");
		if(StrUtils.isNotEmpty(content)) stringBuilder.append(" and o.content like :content ");
		if(StrUtils.isNotEmpty(creator)) stringBuilder.append(" and o.creator like :creator ");
		if(beginTime != null) stringBuilder.append(" and o.createTime > :beginTime ");
		if(endTime != null)	stringBuilder.append(" and o.createTime < :endTime ");
		stringBuilder.append(" order by o.createTime desc");
		
		Query query = getSession().createQuery(stringBuilder.toString());
		
		if(StrUtils.isNotEmpty(title)) query.setParameter("title", "%" + title + "%");
		if(StrUtils.isNotEmpty(content)) query.setParameter("content", "%" + content + "%");
		if(StrUtils.isNotEmpty(creator))	query.setParameter("creator", "%" + creator + "%");
		if(beginTime != null)	query.setParameter("beginTime", beginTime);
		if(endTime != null)		query.setParameter("endTime", endTime);
		
		return (Long) query.uniqueResult();
	}
}
